package com.jsh.erp.utils;


import java.util.Calendar;
import java.util.UUID;

/**
* @Author: wy
* @Description: 生成id
* @DateTime: 2022/7/23 17:36
* @Params:
* @Return
*/
public class IdUtils {

    private static final long FIX_2020_DATE_TIME = 1577548800000L;


    public static String getUUID(){
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String idDesc=genId()+uuid;
        return idDesc;
    }

    public static String genId() {
        long curTime = Calendar.getInstance().getTimeInMillis();
        String barCode = "" + (curTime - FIX_2020_DATE_TIME);
        if(barCode.length() == 11) {
            barCode = "1" + barCode;
        }
        return barCode+ barcodeJiaoYan(barCode);
    }

    public static Long getTimeMill() {

        return Calendar.getInstance().getTimeInMillis();
    }

    /**
     * EAN-13校验位计算
     *
     * @param barcode
     * @return
     */
    private static int barcodeJiaoYan(String barcode) {
        if (barcode.length() != 12)
            return -1;
        int s1 = 0;
        int s2 = 0;
        for (int i = 1; i <= 12; i++) {
            if (i % 2 == 1) {
                s1 = s1 + Integer.parseInt(barcode.substring(i - 1, i));// 奇数
            } else {
                s2 = s2 + Integer.parseInt(barcode.substring(i - 1, i));// 偶数
            }
        }
        s2 = s2 * 3;
        int c = 10 - (s1 + s2) % 10;
        return c == 10 ? 0 : c;
    }



    public static void main(String[] args) {
        //未加工的UUID
//        String preUuid = UUID.randomUUID().toString();
//
//        //第一种方法生成UUID，去掉“-”符号
//        System.out.println(UUID.randomUUID().toString().replace("-", ""));
//        System.out.println(UUID.randomUUID().toString().replace("-", ""));
//        System.out.println(UUID.randomUUID().toString().replace("-", ""));
//        System.out.println(UUID.randomUUID().toString().replace("-", ""));
//        System.out.println(UUID.randomUUID().toString().replace("-", ""));
//        System.out.println(UUID.randomUUID().toString().replace("-", ""));
//        System.out.println(UUID.randomUUID().toString().replace("-", ""));
//        System.out.println(UUID.randomUUID().toString().replace("-", ""));
//
//        //未加工的UUID
//        String preUuid2 = UUID.randomUUID().toString();
//        //第二种生成UUID的方法，去掉“-”符号
//        String changUuid = preUuid2.substring(0,8)+preUuid2.substring(9,13)+preUuid2.substring(14,18)+preUuid2.substring(19,23)+preUuid2.substring(24);
//
//        System.out.println(changUuid);

//        String value ="[0][\"225\"][\"210\"][\"211\"][\"259\"][\"260\"][\"263\"][\"32\"][\"241\"][\"33\"][\"199\"][\"38\"][\"242\"][\"41\"][\"200\"][\"239\"][\"201\"][\"202\"][\"40\"][\"232\"][\"233\"][\"261\"][\"262\"][\"44\"][\"197\"][\"203\"][\"204\"][\"205\"][\"206\"][\"212\"][\"198\"][\"246\"][\"207\"][\"208\"][\"209\"][\"226\"][\"227\"][\"248\"][\"228\"][\"229\"][\"59\"][\"235\"][\"237\"][\"244\"][\"21\"][\"22\"][\"23\"][\"220\"][\"247\"][\"24\"][\"25\"][\"217\"][\"218\"][\"26\"][\"194\"][\"195\"][\"31\"]";
//        String newValue = value.replaceAll(",","\\]\\[");
//        newValue = newValue.replaceAll("\\[0\\]","").replaceAll("\\[\\]","");
//        newValue=newValue.replaceAll("\"", "");
//        System.out.println(newValue);;

//        String i="930be1b2c7c4465383c245dc575ea962";
//        String y="0820345673932c8daed90511c41719460874a495b80b4";
//        System.out.println(i.length()+"i"+y.length());
//        long curTime = Calendar.getInstance().getTimeInMillis();
//        System.out.println("Time"+curTime);
//        String barCode = "" + (curTime - FIX_2020_DATE_TIME);
//        System.out.println("barCode"+barCode);
        String uuid = getUUID();
        System.out.println(uuid);

    }
}
